package org.sfnelson.sk.client.rpc;

import java.util.HashSet;
import java.util.Set;

import org.sfnelson.sk.client.event.Created;
import org.sfnelson.sk.client.event.Event;
import org.sfnelson.sk.client.event.Joined;
import org.sfnelson.sk.client.event.Left;
import org.sfnelson.sk.client.event.ReceivedLoot;

public class EventManager {
	
	private static EventManager instance;
	
	public static EventManager impl() {
		return instance;
	}
	
	protected EventManager() {
		instance = this;
	}
	
	private final Set<EventListener> listeners = new HashSet<EventListener>();

	public void addListener(EventListener listener) {
		listeners.add(listener);
	}
	
	public void removeListener(EventListener listener) {
		listeners.remove(listener);
	}
	
	protected void event(Event event) {
		if (event instanceof Created) created((Created) event);
		else if (event instanceof Joined) joined((Joined) event);
		else if (event instanceof Left) left((Left) event);
		else if (event instanceof ReceivedLoot) receivedLoot((ReceivedLoot) event);
	}
	
	protected void created(Created event) {
		for (EventListener listener: listeners) {
			listener.onCreated(event);
		}
	}
	
	protected void joined(Joined event) {
		for (EventListener listener: listeners) {
			listener.onJoined(event);
		}
	}
	
	protected void left(Left event) {
		for (EventListener listener: listeners) {
			listener.onLeft(event);
		}
	}
	
	protected void receivedLoot(ReceivedLoot event) {
		for (EventListener listener: listeners) {
			listener.onReceivedLoot(event);
		}
	}
}
